<?php
class wordpaster {
	public $plugin_path = "";
	
	public function __construct()
	{
		$this->plugin_path = plugins_url('/',__FILE__);
        add_action( 'admin_head', array($this,'w_add_mce_button') );
	}
	
	public function deactivate()
	{
		global $current_user;
		//update_user_option($current_user->ID, 'rich_editing', 'true', true);
        //delete_option('ke_auto_highlight');
        //delete_option('ke_highlight_type');
	}

	public function activate()
	{
		global $current_user;
		//update_user_option($current_user->ID, 'rich_editing', 'true', true);
        //add_option('ke_auto_highlight', '');
        //add_option('ke_highlight_type', 'prettify');
	}
	
	public function load_wordpaster()
	{
		?><div id="wordpaster-container"></div>
		<script type="text/javascript">
		//<![CDATA[
            jQuery(document).ready(function($){
                window.$ = $;
                WordPaster.getInstance({
                    //上传接口配置教程：http://www.ncmem.com/doc/view.aspx?id=d88b60a2b0204af1ba62fa66288203ed
                    PostUrl: "<?php echo($this->plugin_path) ?>upload.php",
                    License2:"<?php echo(get_option('wordpaster_license'))?>",
                    ImageUrl:"<?php echo(site_url()) ?>{url}",
                    ui:{render:"wordpaster-container"}
                });//加载控件
            });
		//]]>
		</script>
		<?php
	}
	
	public function user_personalopts_update()
    {
        global $current_user;
        //update_user_option($current_user->ID, 'rich_editing', 'false', true);
    }
	
	public function add_admin_js()
	{
		wp_enqueue_script('json', $this->plugin_path . 'js/json2.min.js');
		wp_enqueue_script('skygqbox', $this->plugin_path . 'js/skygqbox.js');
		wp_enqueue_script('wordpaster-edge', $this->plugin_path . 'js/w.edge.js');
		wp_enqueue_script('wordpaster-app', $this->plugin_path . 'js/w.app.js');
		wp_enqueue_script('wordpaster-file', $this->plugin_path . 'js/w.file.js');
		wp_enqueue_script('wordpaster', $this->plugin_path . 'js/w.js');
	}
	
	public function add_admin_style()
	{	
		$w_css = plugins_url('css/w.css', __FILE__);
		$skygqbox_css = plugins_url('js/skygqbox.css', __FILE__);
		wp_register_style('w-css', $w_css);
		wp_register_style('skygqbox-css', $skygqbox_css);
		wp_enqueue_style('w-css');
		wp_enqueue_style('skygqbox-css');
	}
	
	public function add_head_script()
	{
        //wp_enqueue_script('jquery');
	    //wp_enqueue_script('prettify-js', $this->plugin_path .'plugins/code/prettify.js','','20110329');
        ?>
        <script type="text/javascript">
             //window.onload = function(){
                 //prettyPrint();
             //}
        </script>
        <?php
	}

    public function add_head_style()
    {
    	//$type = get_option('ke_highlight_type');
        //wp_enqueue_style('prettify-css', $this->plugin_path .'plugins/code/' . $type . '.css','','20110329');
    }
	
    function w_add_mce_button() {
        // 校验权限
        if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
            return;
        }

        if ( get_user_option( 'rich_editing' ) == 'true' ) 
        {
            // 添加钩子
            add_filter("mce_external_plugins", array(&$this,"w_add_tinymce_plugin") );
            add_filter("mce_buttons", array(&$this,"w_add_tinymce_buttons") );
        }
    }

    // 关联js文件
    function w_add_tinymce_plugin($pluarray)
    {
        $plugin_array["wordpaster_button"] =  $this->plugin_path .'tinymce4/wordpaster.js';
        return $plugin_array;
    }

    function w_add_tinymce_buttons($buttons)
    {
        array_push($buttons, "wordpaster");
        array_push($buttons, "pptpaster");
        array_push($buttons, "pdfimport");
        array_push($buttons, "netpaster");
        return $buttons;
    }

	public function add_admin_head()
    {
    }
}

$wordpaster = new wordpaster();
?>